<?php defined('SYSPATH') or die('No direct access allowed.');


class Model_Ferry_Suit extends ORM
{
    /**
     * 获取套餐详情
     * @param $suit_id
     * @return null
     */
    public static function suit_info($suit_id)
    {
        $suit = DB::select()->from('ferry_ticket')->where('id','=',$suit_id)->execute()->current();
        if(empty($suit)){
            return null;
        }
        $suit_formats=self::format_suit_info(array($suit));
        $suit=$suit_formats[0];
        $suit['dingjin'] = Currency_Tool::price($suit['dingjin']);
        $suit['paytype_name'] = Model_Member_Order::get_paytype_name($suit['paytype']);
        $tickettype_name = DB::select('kindname')
            ->from('ferry_ticket_type')
            ->where('id','=',$suit['tickettypeid'])->execute()->get('kindname');
        $suit['tickettype_name'] = $tickettype_name;

        //产品编号
        $suit['series'] = St_Product::product_series($suit['ferryid'], 5);
		$suit['ferry_name'] = DB::select()->from('ferry')->where('id','=',$suit['ferryid'])->execute()->get('title');

        $suit['book_tips']=self::calc_days($suit);


        return $suit;
    }

    /**
     * 处理景点预订天数说明
     * @param $item
     * @return string
     */
    public static function calc_days($item){
        $time = time();
        $book_tips = '';
        if ($item['day_before'] == 0) {
            // 小时分钟均为00时，表示可以在23：59：59秒前预订
            if ($item['hour_before'] == 0 && $item['minute_before'] == 0) {
                $item['hour_before']   = 23;
                $item['minute_before'] = 59;
            }
            $book_tips = $time < strtotime(date('Y-m-d') . " {$item['hour_before']}:" . ($item['minute_before'] ? $item['minute_before'] : '00')) ? '可订今日票' : '可订明日票';
        } elseif ($item['day_before'] == 1) {

            if ($time < strtotime(date('Y-m-d') . " {$item['hour_before']}:" . ($item['minute_before'] ? $item['minute_before'] : '00')) || !$item['hour_before']) {
                $book_tips = '可订明日票';
            } else {
                $book_tips = '可订后日票';
            }
        } elseif ($item['day_before'] > 1) {
            if ($time < strtotime(date('Y-m-d') . " {$item['hour_before']}:" . ($item['minute_before'] ? $item['minute_before'] : '00')) || !$item['hour_before']) {
                $item['book_tips'] = sprintf('可订%s票', date('m月d日', strtotime("+{$item['day_before']} days")));
            } else {
                $item['day_before']++;
                $item['book_tips'] = sprintf('可订%s票', date('m月d日', strtotime("+{$item['day_before']} days")));
            }
        }
        return $book_tips;
    }

    public static function format_suit_info($suits)
    {
        foreach ($suits as &$v)
        {
            $v['supplierlist'] = $v['supplierlist'] ? : 0;
            //供应商
            if(Model_Supplier::display_is_open()&&$v['supplierlist'])
            {
                $supplier = DB::select('id','litpic','suppliername')->from('supplier')->where('id','=',$v['supplierlist'])->execute()->current();
                $v['suppliers'] = $supplier;
            }else{
                $v['suppliers'] = [];
            }
            $priceArr = Model_Ferry::get_minprice($v['ferryid'], $v['id']);
            $v['ourprice'] = $priceArr['price'];//最低价
            $v['sellprice'] = $priceArr['sellprice'];//原价
            //报价状态
            $price_status=Model_Ferry_Ticket_Price::get_price_status($v);
            $v['price_status'] = $price_status;
            $tickettype_name = DB::select('kindname')->from('ferry_ticket_type')
                ->where('id','=',$v['tickettypeid'])->execute()->get('kindname');
            $v['tickettype_name'] = $tickettype_name;
            $v['paytype_name'] = Model_Member_Order::get_paytype_name($v['paytype']);
            $v['day_before_des']='';
            if(!empty($v['day_before']) || !empty($v['hour_before']) || !empty($v['minute_before']))
            {
                $day_before_des = !empty($v['day_before'])?'提前'.$v['day_before'].'天':'当天';
                $day_before_des_mobile = !empty($v['day_before'])?'提前'.$v['day_before'].'天':'当天';
                $hour_before = intval($v['hour_before']);
                $minute_before = intval($v['minute_before']);
                if(!empty($hour_before) || !empty($minute_before))
                {
                    $hour_before = $hour_before < 10 ? '0' . $hour_before : $hour_before;
                    $minute_before = $minute_before < 10 ? '0' . $minute_before : $minute_before;
                    $day_before_des .= $hour_before . ':' . $minute_before;
                    $day_before_des_mobile.=  $hour_before . ':' . $minute_before;
                    $day_before_des.='前';
                    $day_before_des_mobile.='前';
                }
                else if(!empty($v['day_before']))
                {
                    $day_before_des.='24:00前';
                    $day_before_des_mobile.='24:00前';
                }

                $day_before_des_mobile.=!empty($v['day_before'])?'预订门票':'可预订当日票';

                $v['day_before_des'] = $day_before_des;
                $v['day_before_des_mobile'] = $day_before_des_mobile;
            }
            //验票有效期
            $v['effective_before_days_des']='';
            if (!empty($v['effective_days']))
            {
                if($v['effective_days']==-1)
                {
                    $day_before_des='验票当天24:00前';
                }
                else
                {
                    $day_before_des='游客指定入园日期后'.$v['effective_days'].'天内有效';
                }
                $v['effective_before_days_des'] = $day_before_des;
            }

        }
        return $suits;
    }
}